package com.ac.input.dao.impl;

import java.util.List;

import com.caac.core.util.StringUtil;
import com.ac.input.dao.ReportsDao;
import com.ac.input.dm.HndInpDef;
import com.ac.input.dm.HndInpDefItm;
import com.ac.input.dm.HndLog;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright AgileCentury 2004</p>
 * <p>Company: AgileCentury</p>
 * @author <a href="mailto:lianzs@tom.com">Lianzs</a>
 * @version 1.0
 */

public class ReportsDaoImpl extends BaseDaoImpl implements ReportsDao {
    private static final String SQL_HAD_INP_DEF1 = "SELECT * FROM HND_INP_DEF WHERE INP_CODE=?";
    private static final String SQL_HAD_ITM_DEF1 = "SELECT * FROM HND_INP_DEF_ITM WHERE INP_CODE = ? ORDER BY VIS_NO";
    private static final String SQL_HAD_ITM_DEF2 = "SELECT * FROM HND_INP_DEF_ITM WHERE INP_CODE=? AND COL_CODE=?";
    private static final String SQL_HND_LOG_INSERT = "INSERT INTO HND_LOG(OPT_TIME,ID,GRP_NAME,SUB_NAME,OPERATOR,OPT_DESC) VALUES(SYSDATE,?,?,?,?,?)";
    private static final String SQL_HND_LOG_SELECT = "SELECT * FROM HND_LOG A WHERE A.GRP_NAME=? AND A.SUB_NAME=? ORDER BY A.OPT_TIME DESC";

    public HndInpDef getHndInpDefById(String groupName) {
        HndInpDef dm = (HndInpDef) this.getObject(SQL_HAD_INP_DEF1,
                new String[] { groupName }, HndInpDef.class);
        if (dm != null) {
            dm.setCols(getHndInpDefItms(groupName));
        }
        return dm;
    }

    public HndInpDefItm getHndInpDefItm(String inpCode, String colCode) {
        HndInpDefItm dm = (HndInpDefItm) this.getObject(SQL_HAD_ITM_DEF2,
                new String[] { inpCode, colCode }, HndInpDefItm.class);
        if (dm != null) {
            if ("4".equals(dm.getVisTyp())) {
                dm.setListData(this.getObjects(dm.getListSql()));
            }
        }
        return dm;
    }

    public List getHndInpDefItms(String groupName) {
        List dmList = this.getObjects(SQL_HAD_ITM_DEF1,
                new String[] { groupName }, HndInpDefItm.class);
        for (int i = 0; i < dmList.size(); i++) {
            HndInpDefItm dm = (HndInpDefItm) dmList.get(i);
            if ("4".equals(dm.getVisTyp())) {
                //dm.setListData(this.getObjects(dm.getListSql()));
                dm.setListData(this.getObjects(dm.getListSql()));
            }
        }
        return dmList;
    }

    public List getHndLogs(String groupName, String subName) {
        return this.getObjects(SQL_HND_LOG_SELECT, new String[] { groupName,
                subName }, HndLog.class);
    }

    public void saveHndLog(HndLog hndLog) {
        //        String sql = "INSERT INTO HND_LOG(OPT_TIME,ID,GRP_NAME,SUB_NAME,OPERATOR,OPT_DESC) VALUES(SYSDATE,'"
        //                     + com.fulong.input.util.UUID.randomUUID() + "','"
        //                     + hndLog.getGrpName() + "','"
        //                     + hndLog.getSubName() + "','"
        //                     + hndLog.getOperator() + "','"
        //                     + StringUtil.toSQL(hndLog.getOptDesc()) + "')";
        //        logger.info(this.toString() + "--saveWorkLog(...)" + sql);
        //        this.getJdbcTemplate().execute(sql);
        this.getJdbcTemplate().update(
                SQL_HND_LOG_INSERT,
                new Object[] {
                        com.ac.input.util.UUID.randomUUID().toString(),
                        hndLog.getGrpName(), hndLog.getSubName(),
                        hndLog.getOperator(),
                        StringUtil.toSQL(hndLog.getOptDesc()) });
    }
}
